package com.lianzhihui.romeex.ui.account;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.MnemonicsWordAdapter;
import com.lianzhihui.romeex.base.AppConfig;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.MyToast;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.StringTool;
import com.lianzhihui.romeex.utils.TextUtil;
import com.lianzhihui.romeex.utils.UserUtils;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 导出助记词/私钥/keystone
 * Author: RAMON
 * CreateDate: 2020/9/23 3:14 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class ExportWordPKActivity extends BaseActivity {

    @BindView(R.id.addressTv)
    TextView addressTv;

    @BindView(R.id.nodataTv)
    TextView nodataTv;

    @BindView(R.id.qr_icon)
    ImageView qr_icon;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_private_key)
    TextView tv_private_key;

    @BindView(R.id.copy_bt)
    TextView copy_bt;

    @BindView(R.id.copy_bt2)
    TextView copy_bt2;

    @BindView(R.id.tv_keystore)
    TextView tv_keystore;

    @BindView(R.id.mnemonic_create_lin_pk)
    LinearLayout mLinPrivateKey;

    @BindView(R.id.mnemonic_create_ll_root)
    LinearLayout ll_root;

    String address;
    WalletInfo wallet;
    List<String> words;

    private MnemonicsWordAdapter adapter = null;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_export_wordspk_layout;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        initViewUI();

        Intent intent = getIntent();
        address = intent.getStringExtra(AppConfig.ADDRESS);
        if (TextUtils.isEmpty(address)) {
            MyToast.showToast("无地址");
            finish();
        }

        addressTv.setText(StringTool.addHead(address));
        wallet = UserUtils.getUser(address);
        String pk = wallet.getPrivateKey();
        tv_private_key.setText(pk);

        tv_keystore.setText(wallet.getCiphertext());
        LogUtil.error("ramon", "words: " + wallet.getMnemonicWords());
        if (!TextUtils.isEmpty(wallet.getMnemonicWords())) {
            words = Arrays.asList(wallet.getMnemonicWords().split(" "));

            LogUtil.error("ramon", "size: " + words.size() + "");
            if (adapter == null) {
                adapter = new MnemonicsWordAdapter(words);
                mRecyclerView.setHasFixedSize(true);
                mRecyclerView.setNestedScrollingEnabled(false);
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                mRecyclerView.setAdapter(adapter);
            } else {
                adapter.setNewData(words);
                nodataTv.setVisibility(View.GONE);
            }
        } else {
            nodataTv.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {

    }

    private void initViewUI() {
        setTitle(getString(R.string.exprot_title));
        tv_private_key.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_private_key.getPaint().setAntiAlias(true);//抗锯齿
    }

    @OnClick({R.id.copy_bt, R.id.mnemonic_create_lin_private_key,
            R.id.qr_icon, R.id.copy_bt2})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.qr_icon:
                //跳转 私钥二维码
                if (NoDoubleClickUtils.isDoubleClick()) {
                    Intent intent = new Intent(this, PKQRActivity.class);
                    intent.putExtra(AppConfig.PRIVATE_KEY, wallet.getPrivateKey());
                    startActivity(intent);
                }
                break;
            case R.id.mnemonic_create_lin_private_key:
                if (NoDoubleClickUtils.isDoubleClick()) {
                    copyString(tv_private_key.getText().toString().trim());
                }
                break;
            case R.id.copy_bt:
                copyString(wallet.getPrivateKey());
                break;
            case R.id.copy_bt2:
                copyString(wallet.getCiphertext());
                break;
            default:
                break;
        }
    }

}
